"""常用工具函数"""
import json
import time
from functools import wraps

from air_web.web_flask.bll.base_bll import BaseBll
from air_web.web_flask.common.constants import Status, StatusMsg


def get_default_result():
    """
    获取默认返回的dict
    :return:
    """
    return {
        "status": Status.OK,
        "data": {"total": 0, "pageSize": 0, "pageNum": 0, "result": []},
        "msg": StatusMsg.OK,
        "time": int(time.time()),
    }


def check_es(func):
    """
    检测es状态
    """

    @wraps(func)
    def check_es_wrapper_func(*args, **kwargs):
        if BaseBll(init_es_dal=True).ping_es():
            return func(*args, **kwargs)
        else:
            res = get_default_result()
            res["status"] = 9
            res["msg"] = "es服务挂起，请联系管理员"
            return json.dumps(res)

    return check_es_wrapper_func
